package xuegao.study.mall.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.admin.feign.UmsRoleClient;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.ums.UmsMenu;
import xuegao.study.mall.common.model.ums.UmsResource;
import xuegao.study.mall.common.model.ums.UmsRole;

import java.util.List;
import java.util.Map;

@Api(tags = "UmsRoleController")
@Tag(name = "UmsRoleController", description = "角色管理相关接口")
@RestController
@RequestMapping("/role")
public class UmsRoleController {

    @Autowired
    private UmsRoleClient umsRoleClient;

    @ApiOperation("增加角色")
    @PostMapping("/add")
    public CommonResult<Integer> add(@RequestBody UmsRole umsRole) {
        return umsRoleClient.add(umsRole);
    }
    @ApiOperation("更新角色")
    @PostMapping("/update/{id}")
    public CommonResult<Integer> update(@PathVariable("id") Long id, @RequestBody UmsRole umsRole) {
        return umsRoleClient.update(id, umsRole);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Integer> deleteResource(@PathVariable("id") Long id) {
        return umsRoleClient.deleteRole(id);
    }

    @ApiOperation("角色列表查询")
    @GetMapping("/list")
    public CommonResult<CommonPage<UmsRole>> listRole(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "name", defaultValue = "") String name){
        return umsRoleClient.listRole(pageNum, pageSize, name);
    }

    @ApiOperation("查询所有角色")
    @GetMapping("/listAll")
    public CommonResult<List<UmsRole>> listAll() {
        return umsRoleClient.listAll();
    }

    @ApiOperation("角色资源映射列表")
    @GetMapping("/initResourceRolesMap")
    public CommonResult<Map<String, List<String>>> initResourceRolesMap() {
        return umsRoleClient.initResourceRolesMap();
    }

    @ApiOperation("查询该角色菜单列表")
    @GetMapping("/listMenu/{roleId}")
    public CommonResult<List<UmsMenu>> listMenu(@PathVariable("roleId") Long roleId) {
        return umsRoleClient.listMenu(roleId);
    }

    @ApiOperation("查询该角色资源列表")
    @GetMapping("/listResource/{roleId}")
    public CommonResult<List<UmsResource>> listResource(@PathVariable("roleId") Long roleId) {
        return umsRoleClient.listResource(roleId);
    }

    @ApiOperation("给角色分配菜单")
    @PostMapping("/allocMenu")
    public CommonResult<Integer> allocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {
        return umsRoleClient.allocMenu(roleId, menuIds);
    }

    @ApiOperation("给角色分配资源")
    @PostMapping("/allocResource")
    public CommonResult<Integer> allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        return umsRoleClient.allocResource(roleId, resourceIds);
    }
}
